package com.xai.pg.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @Author xxx
 * @Date 2018-12-24 15:27:58
 * @Desc Conf
 */
public class Conf {

    private static Properties prop;

    private static final String URL_FMT = "jdbc:mysql://%s:%s/%s?useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
    private static final String CONF_PATH = "/Users/admin/develop/work/github/ProGenerator/conf.properties";
//    private static final String CONF_PATH = "./conf.properties";

    static {
        InputStream inStream = null;
        try {
            inStream = new FileInputStream(new File(CONF_PATH));
            prop = new Properties();
            prop.load(inStream);

            String type = prop.getProperty("pro.type");
            VMUtil.jpa = (type != null && "jpa".equals(type.trim().toLowerCase()));

            String apiStyle = prop.getProperty("build.api.style");
            VMUtil.isRestful = (apiStyle != null && "restful".equals(apiStyle.trim().toLowerCase()));

            String receiveBody = prop.getProperty("receive.body");
            VMUtil.receiveBody = (receiveBody != null && "true".equals(apiStyle.trim().toLowerCase()));
            Logger.getLogger(Conf.class.getName()).log(Level.INFO, "IS JPA -> {0}", VMUtil.jpa);
            Logger.getLogger(Conf.class.getName()).log(Level.INFO, "IS RESTFUL -> {0}", VMUtil.jpa);
            Logger.getLogger(Conf.class.getName()).log(Level.INFO, "RECEIVE BODY-> {0}", VMUtil.jpa);

        } catch (Exception ex) {
            Logger.getLogger(Conf.class.getName()).log(Level.SEVERE, "请先修改conf.properties配置文件中的信息,再修改util包中的 Conf.java 中的 CONF_PATH 为能找到的配置文件路径");
            Logger.getLogger(Conf.class.getName()).log(Level.SEVERE, "Please modify the information in the conf.properties configuration file, and then modify the CONF_PATH in Conf.java in the util package to find the configuration file path.");
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException ex) {
                    Logger.getLogger(Conf.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void init() {
    }

    public static String getDBUrl() {
        if (prop == null) {
            return "";
        }

        String host = prop.getProperty("db.host");
        String port = prop.getProperty("db.port");
        String name = prop.getProperty("db.name");
        return String.format(URL_FMT, host, port, name);
    }

    public static String getDBName() {
        return Conf.get("db.name");
    }

    public static String getDBUser() {
        return Conf.get("db.user");
    }

    public static String getDBPass() {
        return Conf.get("db.password");
    }

    public static String getGroup() {
        return Conf.get("pro.base.package");
    }

    public static String get(String name) {
        if (prop == null) {
            return "";
        }
        return prop.getProperty(name);
    }

    private static String basePath = null;

    public static String getBasePath() {
        if (basePath == null) {
            String proName = Conf.get("pro.name");
            String proPath = Conf.get("pro.save.path");
            basePath = proPath + "/" + proName;
        }
        return basePath;
    }

    private static String javaBaseDir = null;

    public static String getJavaBasePath() {
        if (javaBaseDir == null) {
            String basePackage = Conf.getGroup();
            String javaFilePath = basePackage.replace(".", "/");
            javaBaseDir = getBasePath() + "/src/main/java/" + javaFilePath;
        }
        return javaBaseDir;
    }
}
